package com.cskaoyan.market.admin.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cskaoyan.market.admin.service.AdminRegionService;
import com.cskaoyan.market.admin.service.impl.AdminRegionServiceImpl;
import com.cskaoyan.market.admin.vo.RegionVo;
import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.ResponseUtil;

/**
 * 行政区域控制层
 *
 * @author BlackFlame33
 * @date 2023/03/20
 **/
@WebServlet("/admin/region/list")
public class AdminRegionController extends HttpServlet {
    private AdminRegionService adminRegionService = new AdminRegionServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<MarketRegion> regionlist = adminRegionService.getRegionList();
        Map<Byte, List<MarketRegion>> regionsGroupByByte = groupByType(regionlist);
        // 根据type的类型来获取省、市、县的数据，那么我们可以设计一个常量来获取1,2，3等数值
        List<MarketRegion> provinceList = regionsGroupByByte.get(MarketConstant.MARKET_TYPE_PROVINCE);
        List<MarketRegion> cityList = regionsGroupByByte.get(MarketConstant.MARKET_TYPE_CITY);
        List<MarketRegion> areaList = regionsGroupByByte.get(MarketConstant.MARKET_TYPE_AREA);
        // 根据响应的要求，city和area的list数据还需要根据pid的值来进一步分组划分
        Map<Integer, List<MarketRegion>> cityRegionsGroupByPid = groupByPid(cityList);
        Map<Integer, List<MarketRegion>> areaRegionsGroupByPid = groupByPid(areaList);
        List<RegionVo> list = new ArrayList<>();
        for (MarketRegion province : provinceList) {
            // 遍历出来的便是每一个省份
            RegionVo provinceRegionVo = transferDOtoVO(province);
            List<MarketRegion> cities = cityRegionsGroupByPid.get(province.getId());
            List<RegionVo> childrenOfProvince = new ArrayList<>();
            for (MarketRegion city : cities) {
                // 遍历出来的是当前省份下面的每一个城市，比如河北省下面的石家庄市，需要查找石家庄市的区
                RegionVo cityRegionVo = transferDOtoVO(city);
                List<MarketRegion> areas = areaRegionsGroupByPid.get(city.getId());
                List<RegionVo> childrenOfCity = new ArrayList<>();
                for (MarketRegion area : areas) {
                    RegionVo areaRegionVo = transferDOtoVO(area);
                    childrenOfCity.add(areaRegionVo);
                }
                cityRegionVo.setChildren(childrenOfCity);
                childrenOfProvince.add(cityRegionVo);
            }
            provinceRegionVo.setChildren(childrenOfProvince);
            list.add(provinceRegionVo);
        }
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.okList(list)));
    }

    private RegionVo transferDOtoVO(MarketRegion region) {
        RegionVo regionVo = new RegionVo();
        regionVo.setId(region.getId());
        regionVo.setName(region.getName());
        regionVo.setType(region.getType());
        regionVo.setCode(region.getCode());
        return regionVo;
    }

    private Map<Integer, List<MarketRegion>> groupByPid(List<MarketRegion> regions) {
        Map<Integer, List<MarketRegion>> result = new HashMap<>();
        for (MarketRegion region : regions) {
            List<MarketRegion> regionList = result.computeIfAbsent(region.getPid(), k -> new ArrayList<>());
            regionList.add(region);
        }
        return result;
    }

    private Map<Byte, List<MarketRegion>> groupByType(List<MarketRegion> marketRegions) {
        Map<Byte, List<MarketRegion>> result = new HashMap<>();
        for (MarketRegion marketRegion : marketRegions) {
            // 根据type类型分类，1,2,3将list加以分类
            List<MarketRegion> regionList = result.computeIfAbsent(marketRegion.getType(), k -> new ArrayList<>());
            regionList.add(marketRegion);
        }
        return result;
    }
}